function [ber, numBits] = LDPC_PPM_BERtool2(EbNo, maxNumErrs, maxNumBits)

% Import Java class for BERTool.
import com.mathworks.toolbox.comm.BERTool;

% Initialize variables related to exit criteria. Stuff: k,frame,lognormal variance
totErr  = 0; % Number of errors observed
numBits = 0; % Number of bits processed
codeRate = 1/2;
k = 2;

% Number of frame (N bits per frame)
frame = 5;

% Adjust SNR for coded bits and multi-bit symbols.
adjSNR = EbNo - 10*log10(1/codeRate) + 10*log10(k);

% Simulate until number of errors exceeds maxNumErrs
% or number of bits processed exceeds maxNumBits.
while((totErr < maxNumErrs) && (numBits < maxNumBits))

   % Check if the user clicked the Stop button of BERTool.
   if (BERTool.getSimulationStop)
      break;
   end
  
    M = 100;
    N = 200;

    % Number of iteration;
    iter = 5;

    % Make the LDPC matrix
    H = makeLdpc(M, N, 1, 1, 3);

    % Make random data (0/1)
    % The total length of data is frame*M
    dSource = round(rand(M, frame));

    for j = 1:frame

      % Encoding message
      dSource(:, j);
      [c, newH] = makeParityChk(dSource(:, j), H, 2);
      u = [c; dSource(:, j)];

      a = ppm_mod(4, u);
      b = apply_lognormal(.001, a);
      c = awgn(b, adjSNR, 'measured', [], 'dB');
      d = ppm_demod(4, c);

      % Decoding (select decoding method)
      %vhat = decodeProbDomain(d, newH, N0, iter);
      %vhat = decodeLogDomain(tx, newH, N0, iter);
      vhat = decodeLogDomainSimple(d', newH, iter);
      %vhat = decodeBitFlip(tx, newH, iter);

      % Get bit error rate (for brevity, BER calculation includes parity bits)
      [num, rat] = biterr(vhat', u);

    end
    numBits = numBits + M;
    totErr = totErr + num;

end % End of loop

ber = totErr/numBits;